//
//  LineLayer.m
//  cocos2dt1
//
//  Created by mmac on 1/17/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "LineLayer.h"


@implementation LineLayer


BOOL not_empty_block_line(block_line_path line){
	if(line.from.x != 0 && line.from.y != 0 && line.to.x != 0 && line.to.y != 0){
		return TRUE;
	} else {
		return FALSE;	
	}
}

- (void) addLine:(block_line_path)line {
	int i;
	for(i = 0; i < 4; i++){
		if(!not_empty_block_line(lines[i])){
			if(line.from.x < 5){
				line.from.x = 5;
			}
			if(line.from.y < 5){
				line.from.y = 5;
			}
			if(line.to.x < 5){
				line.to.x = 5;
			}
			if(line.to.y < 5){
				line.to.y = 5;
			}
			
			if(line.from.x > 475){
				line.from.x = 475;
			}
			if(line.from.y > 283){
				line.from.y = 283;
			}
			if(line.to.x > 475){
				line.to.x = 475;
			}
			if(line.to.y > 283){
				line.to.y = 283;
			}
			
			lines[i] = line;
			
			needDraw = TRUE;
			break;
		}
	}
}

-(void) clearPath: (ccTime) dt{
	[self clearPath];
}


- (void) clearPath {
	needDraw = FALSE;
	
	block_line_path dfl;
	dfl.from = CGPointZero;
	dfl.to = CGPointZero;
	
	int i;
	for(i = 0; i < 4; i++){
		lines[i] = dfl;
	}
}

-(id) init
{
	// always call "super" init
	// Apple recommends to re-assign "self" with the "super" return value
	if( (self=[super init] )) {
		
		// [self clearPath];
		
		// [self schedule: @selector(clearPath:) interval:0.5];
		needDraw = FALSE;
		
	}
	
	return self;
}

-(void) draw
{
	
	if(!needDraw){
		return;
	}
	
	glColor4ub(255, 0, 0, 255);
	glLineWidth(3);
	// CGPoint vertices[] = { ccp(0,0), ccp(50,50), ccp(100,50), ccp(100,100), ccp(50,100) };
	
	int i;
	for(i = 0; i < 4; i++){
		// NSLog(@"draw line (%f,%f) (%f,%f)", lines[i].from.x, lines[i].from.y, lines[i].to.x, lines[i].to.y);
		if(not_empty_block_line(lines[i])){
			drawLine( lines[i].from, lines[i].to );
		}
	}
	
	
	

	// CGSize s = [[Director sharedDirector] winSize];
	
	/*
	// draw a simple line
	// The default state is:
	// Line Width: 1
	// color: 255,255,255,255 (white, non-transparent)
	// Anti-Aliased
	glEnable(GL_LINE_SMOOTH);
	drawLine( ccp(0, 0), ccp(s.width, s.height) );
	
	// line: color, width, aliased
	// glLineWidth > 1 and GL_LINE_SMOOTH are not compatible
	// GL_SMOOTH_LINE_WIDTH_RANGE = (1,1) on iPhone
	glDisable(GL_LINE_SMOOTH);
	glLineWidth( 5.0f );
	glColor4ub(255,0,0,255);
	drawLine( ccp(0, s.height), ccp(s.width, 0) );
	
	// TIP:
	// If you are going to use always the same color or width, you don't
	// need to call it before every draw
	//
	// Remember: OpenGL is a state-machine.
	
	// draw big point in the center
	glPointSize(64);
	glColor4ub(0,0,255,128);
	drawPoint( ccp(s.width / 2, s.height / 2) );
	
	// draw 4 small points
	CGPoint points[] = { ccp(60,60), ccp(70,70), ccp(60,70), ccp(70,60) };
	glPointSize(4);
	glColor4ub(0,255,255,255);
	drawPoints( points, 4);
	
	// draw a green circle with 10 segments
	glLineWidth(16);
	glColor4ub(0, 255, 0, 255);
	drawCircle( ccp(s.width/2,  s.height/2), 100, 0, 10, NO);
	
	// draw a green circle with 50 segments with line to center
	glLineWidth(2);
	glColor4ub(0, 255, 255, 255);
	drawCircle( ccp(s.width/2, s.height/2), 50, CC_DEGREES_TO_RADIANS(90), 50, YES);	

	
	// open yellow poly
	glColor4ub(255, 255, 0, 255);
	glLineWidth(4);
	// CGPoint vertices[] = { ccp(0,0), ccp(50,50), ccp(100,50), ccp(100,100), ccp(50,100) };
	drawPoly( vertices, 5, NO);
	

	// closed purble poly
	glColor4ub(255, 0, 255, 255);
	glLineWidth(2);
	CGPoint vertices2[] = { ccp(30,130), ccp(30,230), ccp(50,200) };
	drawPoly( vertices2, 3, YES);
	
	// draw quad bezier path
	drawQuadBezier(ccp(0,s.height), ccp(s.width/2,s.height/2), ccp(s.width,s.height), 50);
	
	// draw cubic bezier path
	drawCubicBezier(ccp(s.width/2, s.height/2), ccp(s.width/2+30,s.height/2+50), ccp(s.width/2+60,s.height/2-50),ccp(s.width, s.height/2),100);
	*/
	
	// restore original values
	glLineWidth(1);
	glColor4ub(255,255,255,255);
	glPointSize(1);
	
	
}

@end
